package com.pinc.poop;

import com.google.ads.Ad;
import com.google.ads.AdListener;
import com.google.ads.AdRequest;
import com.google.ads.AdView;
import com.google.ads.AdRequest.ErrorCode;
import com.pinc.poop.R;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

public class statistic extends PwdProtectWidgetActivity implements AdListener{

	private DBClass dbHelper;
	private int[] state;
	private static final int interval_hours = 4;
	private static final int startHour = 2;
	private SharedPreferences settings;
	private AdView adview;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	protected void initWidget() {
		int total = 0;

		TextView tv_left = (TextView) this.findViewById(R.id.button_left);
		tv_left.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});

		TextView tv_right = (TextView) this.findViewById(R.id.button_right);
		tv_right.setText("");

		TextView tv_title = (TextView) this.findViewById(R.id.textView_title);
		tv_title.setText(getResources().getString(R.string.str_statistics));

		LinearLayout ll = (LinearLayout) this.findViewById(R.id.linearLayout1);

		for ( int i=startHour; i<startHour+24; i+=interval_hours)
		{
			state[i/interval_hours]= dbHelper.getRecordCount("time LIKE ? OR time LIKE ? OR time LIKE ? OR time LIKE ?", new String[]{String.format("%02d:%%", i%24), String.format("%02d:%%", (i+1)%24), String.format("%02d:%%", (i+2)%24), String.format("%02d:%%", (i+3)%24)});
			total += state[i/interval_hours];
		}


		for ( int i=startHour; i<startHour+24; i+=interval_hours)
		{
			chart_unit nll = new chart_unit(this);
			nll.setChart(state[i/interval_hours], total, i,(i+interval_hours)%24);
			ll.addView(nll);
		}

		adview = (AdView) this.findViewById(R.id.adView);
		adview.setAdListener(this);
	}

	@Override
	protected void onResume() {
		super.onResume();

		setContentView(R.layout.statistic);

		dbHelper = new DBClass(this);
		state = new int[24/interval_hours];

		initWidget();

		/** play ad **/
		adview.setVisibility(View.GONE);
		AdRequest request = new AdRequest();
		settings = PreferenceManager.getDefaultSharedPreferences(this);
		String user_sex_str = settings.getString("user_sex", "null");
		if ( user_sex_str.compareTo(getResources().getStringArray(R.array.str_arr_gender)[0]) == 0)
		{
			request.setGender(AdRequest.Gender.FEMALE);
		}
		else if( user_sex_str.compareTo(getResources().getStringArray(R.array.str_arr_gender)[1]) == 0)
		{
			request.setGender(AdRequest.Gender.MALE);
		}
		adview.loadAd(request);
		/** end play ad **/
	}

	@Override
	public void onDismissScreen(Ad arg0) {
	}

	@Override
	public void onFailedToReceiveAd(Ad arg0, ErrorCode arg1) {
	}

	@Override
	public void onLeaveApplication(Ad arg0) {
	}

	@Override
	public void onPresentScreen(Ad arg0) {
	}

	@Override
	public void onReceiveAd(Ad arg0) {
		adview.setVisibility(View.VISIBLE);
	}

}
